import datetime
import time
from ...core.exception.exception_go import *

class DateGo:
    @staticmethod
    def greenToStandard(sec):

        # _sec = 0

        # if type(sec) == type(int):
        #     _sec = sec
        # elif type(sec) == type(str):
        #     _sec = int(sec)
        # else:
        #     raise Exception("Date Error!")

        result = time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(time.ctime(int(sec)), ))

        return result

    @staticmethod
    def get_current_date():
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        return now

    @staticmethod
    def get_current_datetime():
        now = datetime.datetime.now().strftime('%Y-%m-%d')

        return now

    @staticmethod
    def get_full_datetime():
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %f')

        return now

    @staticmethod
    def get_current_hour():
        return datetime.datetime.now().strftime('%H')

    @staticmethod
    def get_current_date_raw():
        now = datetime.datetime.now()

        return now

    @staticmethod
    def date_befor_days(days, raw=False):
        now_time = datetime.datetime.now()
        # print(now_time.strftime('%Y-%m-%d %H:%M:%S'))

        now_time = now_time - datetime.timedelta(days=int(days))

        if raw == True:
            return now_time
        else:
            return now_time.strftime('%Y-%m-%d')

    @staticmethod
    def date_befor_minutes(value):
        now_time = datetime.datetime.now()
        # print(now_time.strftime('%Y-%m-%d %H:%M:%S'))

        now_time = now_time - datetime.timedelta(minutes=int(value))
        # print(now_time.strftime('%Y-%m-%d %H:%M:%S'))
        return now_time.strftime('%Y-%m-%d %H:%M')

    @staticmethod
    def date_befor_hours(value):
        now_time = datetime.datetime.now()

        now_time = now_time - datetime.timedelta(hours=int(value))
        return now_time.strftime('%Y-%m-%d %H:%M')

    @staticmethod
    def time_to_date(month=None, day=None, hours=None, minutes=None):
        now = datetime.datetime.now()

        _month = month
        _day = day
        _hour = hours
        _minute = minutes

        if month is None:
            _month = now.month
        else:
            if isinstance(month, int) and month < 0:
                _month = now.month + month

        if day is None:
            _day = now.day
        else:
            if isinstance(day, int) and day < 0:
                _day = now.day + day

        if hours is None:
            _hour = now.hour
        else:
            if isinstance(hours, int) and hours < 0:
                _hour = now.hour + hours

        if minutes is None:
            _minute = now.minute
        else:
            if isinstance(minutes, int) and minutes < 0:
                _minute = now.minute + minutes

        dest = datetime.datetime(now.year, int(_month), int(_day), int(_hour), int(_minute))

        return dest.strftime('%Y-%m-%d %H:%M')

    # 03/06/2017 10:45:28
    @staticmethod
    def unify_date(raw):
        if raw == '' or raw is None:
            raise DataFormatException

        format_list = ['%m/%d/%Y %H:%M:%S', '%Y-%m-%d', '%Y-%m-%d %H:%M:%S', '%Y年%m月%d日 %H:%M', '%Y年%m月%d日 %H:%M:%S', '%Y-%m-%d%H:%M:%S']

        t = None
        result = None

        raw = raw.strip()

        for format in format_list:
            try:
                t = time.strptime(raw, format)
                break
            except:
                pass

        if t is not None:
            result = time.strftime('%Y-%m-%d %H:%M:%S', t)
            return result
        else:
            raise DataFormatException

    @staticmethod
    def distance(past):
        # _past = datetime.datetime.strptime('2017-03-5 21:30:00', '%Y-%m-%d %H:%M:%S')
        # _now = datetime.datetime.strptime('2017-03-15 21:30:00', '%Y-%m-%d %H:%M:%S')

        if past is None:
            return 1000000

        _past = past#datetime.datetime.strptime('2017-03-15 23:30:00', '%Y-%m-%d %H:%M:%S')
        _now = datetime.datetime.now()#strptime('2017-03-16 2:30:00', '%Y-%m-%d %H:%M:%S')

        _res = _now - _past
        _day = _res.days

        contain = _res.seconds + _day * 24 * 60 * 60
        dis = int(contain / 60 /60)

        if dis < 0:
            dis = 0

        return dis

    @staticmethod
    def doubleForwardTrim(str):
        str_len = len(str)
        res = None
        skip = False
        for i in range(0, str_len):
            for j in range(0, str_len)[::-1]:
                the_date = str[i:j]
                try:
                    res = DateGo.unify_date(the_date)
                    skip = True
                    break
                except:
                    pass
            if skip:
                break
        return res

    @staticmethod
    def trimDate(str):
        res = None

        try:
            res = DateGo.unify_date(str)
        except:
            res = DateGo.doubleForwardTrim(str)

        if res is None:
            raise  DataFormatException
        else:
            return res

# print(DateGo.get_full_datetime())
# print(DateGo.get_full_datetime())
# print(DateGo.get_full_datetime())
# print(DateGo.get_full_datetime())
# print(DateGo.get_full_datetime())
# print(DateGo.get_full_datetime())
# print(DateGo.greenToStandard(DateGo.get_full_datetime().split(' ')[2]))

# DateGo.distance('2017-03-15 22:30:00')

# dat = '2017-03-1710:03:57来源:网易娱乐'
# dat = '　来源:2017-02-22 09:31:58'
# dat = '　来源: 2017-02-22 09:31:58'
# dat = '来源: 2017-02-22 09:31:58'
# res = DateGo.trimDate(dat)
# print(res)

# one = DateGo.unify_date('01/03/2010 14:41:23')
# two = DateGo.unify_date('2017-05-01')
# three = DateGo.unify_date('2012-01-06 6:43:44')
# four = DateGo.unify_date('2012-01-03 2:44:18')
# five = DateGo.unify_date('2017年03月01日 10:24')
# print(one)

# resu = DateGo.greenToStandard(1120605678)
# resu2 = DateGo.greenToStandard(1314144)

# DateGo.date_compare()

# dat = '上映日期: 2017-05-23(戛纳电影节) / 2017-05-27(日本)'
#
# DateGo.doubleForwardTrim(dat)

